<?php
if (!function_exists('tag_drilldown_url'))
{
	function tag_drilldown_url($tag, &$CI)
	{
		$uri = $CI->uri->uri_to_assoc(2);
		$uri['tagged'] = h($tag->name);
		$str = $CI->uri->assoc_to_uri($uri);
		$str = preg_replace('/\/+/', '/', $str);
		return "/entries/{$str}";
	}
}
?>

<ul>
<?php
if ($tags):
	$is_filtered = (strpos($this->uri->uri_string(), 'entries') !== false) ? true : false;
	foreach ($tags->result() as $tag):
		$user_editable 	= ($allow_edit_tags && $this->userland->logged_in() && $block_instance['klass']->user_can_edit($this->userland->current_user(), $tag));
		$weight			= $block_instance['klass']->get_weight_for($bricabox, $tag);
?>
<li id="tag_<?php echo $tag->id; ?>" class="tags_<?php echo $weight; ?><?php if ($user_editable) echo ' user'; ?>">
	<?php if (!$is_filtered): ?><a href="/entries/tagged/<?php echo form_prep(h($tag->name)); ?>"><?php echo $tag->name; ?></a><?php else: ?><a href="<?php echo tag_drilldown_url($tag, $this); ?>"><?php echo $tag->name; ?></a><?php endif; ?>
	<?php if ($user_editable): ?>
	<span class="controls">
		<a href="#" onclick="return TagsBlock.deleteTag(<?php echo $block_instance['data']->id; ?>, <?php echo $tag->id; ?>, '<?php echo form_prep($tag->name); ?>', <?php echo $content->id; ?>, <?php echo $bricabox->id; ?>);" title="Delete tag &quot;<?php echo $tag->name; ?>&quot;"><img src="/assets/images/icons/cross.png" alt="Remove tag" /></a>
	</span>
	<?php endif; ?>
</li>
<?php
	endforeach;
endif;
?>
</ul>

<?php if ($allow_edit_tags && $this->userland->logged_in()): ?>
<div id="add_tag_<?php echo $block_instance['data']->id; ?>" class="tags_add" >
	<label for="tag_name_<?php echo $block_instance['data']->id; ?>">Add tags (separate with commas):</label>
	<input type="text" name="tag_name" id="tag_name_<?php echo $block_instance['data']->id; ?>" />
	<input type="submit" value="Save" onclick="return TagsBlock.saveTag(<?php echo $block_instance['data']->id; ?>, <?php echo $content->id; ?>, <?php echo $bricabox->id; ?>);" />
</div>
<?php elseif ($allow_edit_tags && !$this->userland->logged_in()): ?>
<a href="/sign_up_or_log_in">sign up or log in to tag this entry</a>
<?php endif; ?>
